package com.his.server.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.his.server.consts.ServerConsts;
import com.his.server.dao.iface.UserInfoDAO;
import com.his.server.exception.DBException;
import com.his.server.utils.dao.DaoUtil;
import com.his.server.utils.main.EnvironmentProperties;

public class UserInfoDAOImpl implements UserInfoDAO {

	public String userName = null;
	public String password = null;
	private int usercount = 0;
	private ResultSet rs = null;
	private PreparedStatement pdst = null;
	private Properties pstmtProperties = null;

	public UserInfoDAOImpl() {

	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUserCount(int userCount) {
		this.usercount = usercount;
	}

	public String getUserName() {
		return this.userName;
	}

	public String getPassword() {
		return this.password;
	}

	public int getUserCount() {
		return this.getUserCount();
	}

	public void doSqlExecute (Connection conn) throws DBException {
		
		pstmtProperties = EnvironmentProperties.getEproperties().getPropertyList().get(1);
		
		try {
			pdst = conn.prepareStatement(
					pstmtProperties.getProperty(ServerConsts.QUERY_USER_INFO_SELECT));
			DaoUtil.setString(pdst, 1, getUserName());
			DaoUtil.setString(pdst, 2, getPassword());

			rs = pdst.executeQuery();

			if (rs.next()) {
				setUserCount(DaoUtil.getInteger(rs, 1));
			}
		} catch (SQLException ex) {
			throw new DBException ("SQLException when execute doSqlExecute", ex);
		}

	}
	
	public void releasePstmt() throws DBException {
		
		if (pdst != null) {
			try {
				pdst.close();
			} catch (SQLException ex) {
				throw new DBException ("SQLException when execute releasePstmt", ex);
			}
		}
	}
}